package com.media.themovieworld.screen.menu;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.media.themovieworld.R;
import com.media.themovieworld.activity.MainActivity;
import com.media.themovieworld.common.fragment.MovieWorldScreen;
import com.media.themovieworld.util.AppSettingPrefference;
import com.media.themovieworld.util.MovieImageLoader;
import com.media.themovieworld.util.dialog.BaseMovieDialog;
import com.media.themovieworld.util.dialog.BaseMovieDialog.OnMovieDialogClickListener;

public class SettingScreen extends MovieWorldScreen implements OnClickListener {

	private TextView tClearCache;
	// private View wraperQuality;
	// private TextView tQuality;
	// private View wraperChroma;
	// private TextView tChroma;
	private Spinner qualitySpinner;
	private Spinner chromaSpinner;
	private Spinner languageSpinner;
	private String mCurrentLanguage;
	
	private boolean isLanguageChange = false;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.tmw__setting_screen, container,
				false);
		qualitySpinner = (Spinner) v
				.findViewById(R.id.tmw__setting_activity_ics_quality_layout_main);
		chromaSpinner = (Spinner) v
				.findViewById(R.id.tmw__setting_activity_ics_chroma_layout_main);
		languageSpinner = (Spinner) v
				.findViewById(R.id.tmw__setting_activity_ics_language_layout_main);
		tClearCache = (TextView) v
				.findViewById(R.id.tmw__setting_activity_text_view_clear_cache);
		tClearCache.setOnClickListener(this);
		mCurrentLanguage = AppSettingPrefference.getInstance().getMovieLanguage();
		// tQuality = (TextView)
		// v.findViewById(R.id.tmw__setting_activity_ics_quality_layout_textview);
		// tChroma = (TextView)
		// v.findViewById(R.id.tmw__setting_activity_ics_chroma_layout_textview);
		// wraperQuality =
		// v.findViewById(R.id.tmw__setting_activity_ics_quality_layout);
		// wraperChroma =
		// v.findViewById(R.id.tmw__setting_activity_ics_chroma_layout);
		initQualitySpinner();
		initChromaSpinner();
		initLanguageSpinner();
		return v;
	}

	// add items into spinner dynamically
	private void initQualitySpinner() {
		String[] list = getResources().getStringArray(R.array.quality);
		final ArrayList<String> listQuality = new ArrayList<String>(Arrays.asList(list));
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_spinner_item, listQuality);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		qualitySpinner.setAdapter(dataAdapter);
		String quality = AppSettingPrefference.getInstance().getMovieQuality();
		int currentQuality = getItemPosition(quality, listQuality);
		qualitySpinner.setSelection(currentQuality == -1 ? 1:currentQuality);
		qualitySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View row,
					int pos, long arg3) {
				AppSettingPrefference.getInstance().setMovieQuality(listQuality.get(pos));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}

	// add items into spinner dynamically
	private void initChromaSpinner() {

		String[] list = getResources().getStringArray(R.array.chroma);
		final ArrayList<String> listChroma = new ArrayList<String>(Arrays.asList(list));
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_spinner_item, listChroma);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		chromaSpinner.setAdapter(dataAdapter);
		String chroma = AppSettingPrefference.getInstance().getMovieChroma();
		int currentChroma = getItemPosition(chroma, listChroma);
		chromaSpinner.setSelection(currentChroma == -1 ? 0:currentChroma);
		chromaSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View row,
					int pos, long arg3) {
				AppSettingPrefference.getInstance().setMovieChroma(listChroma.get(pos));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	private void initLanguageSpinner(){
		String[] list = getResources().getStringArray(R.array.nameLanguage);
		final ArrayList<String> listLanguage = new ArrayList<String>(Arrays.asList(list));
		String[] listValue = getResources().getStringArray(R.array.valueLanguage);
		final ArrayList<String> listLanguageValue = new ArrayList<String>(Arrays.asList(listValue));
		
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
				getActivity(), android.R.layout.simple_spinner_item, listLanguage);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		languageSpinner.setAdapter(dataAdapter);
		String languageValue = AppSettingPrefference.getInstance().getMovieLanguage();
		int currentLanguage = getItemPosition(languageValue, listLanguageValue);
		languageSpinner.setSelection(currentLanguage == -1 ? 0:currentLanguage);
		languageSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View row,
					int pos, long arg3) {
				String language = listLanguageValue.get(pos);
				AppSettingPrefference.getInstance().setMovieLanguage(language);
				if(mCurrentLanguage!=null && mCurrentLanguage.equals(language)){
					isLanguageChange = false;
				}else{
					isLanguageChange = true;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}
	
	public int getItemPosition(String item,ArrayList<String> parent){
		int ret = -1;
		if (item!=null && !item.equals("") && parent!=null) {
			ret = parent.indexOf(item);
		}
		return ret;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tmw__setting_activity_text_view_clear_cache:
			new ClearCacheDialog(getActivity()).show();
			break;

		default:
			break;
		}
	}
	
	@Override
	public String getTitle() {
		return getResources().getString(R.string.tmw__options);
	}

	class ClearCacheDialog extends BaseMovieDialog implements
			OnMovieDialogClickListener {

		public ClearCacheDialog(Context context) {
			super(context);
			setOnMovieDialogClickListener(this);
		}

		@Override
		protected String getDialogTitle() {
			return getContext().getResources().getString(
					R.string.tmw__clear_cache);
		}

		@Override
		protected boolean isSingleConfirmButton() {
			return false;
		}

		@Override
		protected void setTextForButton() {
			setPositiveButtonText(getContext().getResources().getString(
					R.string.yes));
			setNegativeButtonText(getContext().getResources().getString(
					R.string.no));
		}

		@Override
		protected View onCreateContentView(LayoutInflater inflater,
				View contentView) {
			View v = inflater.inflate(
					R.layout.tmw__dialog_comfirm_delete_cache, null);
			return v;
		}

		@Override
		public void onSingleButtonClick(View v) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPositiveButtonClick(View v) {
			MovieImageLoader.getInstance().clearMemoryCache();
			MovieImageLoader.getInstance().clearDiscCache();
			Toast.makeText(
					getContext(),
					getContext()
							.getResources()
							.getString(
									R.string.tmw__setting_activity_clear_cache_successfull),
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onNegativeButtonClick(View v) {

		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if(isLanguageChange == false) return;
		if (getActivity() instanceof MainActivity) {
			((MainActivity)getActivity()).update();
		}
	}
}
